﻿using ORAProvider;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UserService.Entity;
using UserService.IRepository;
using UserService.Repository.Repositories;

namespace UserService.Repository
{
    public class UserStore : IUserStore<User>
    {
        private readonly UserRepository _userRepository;
        public OracleDataContext Database { get; private set; }
        public UserStore()
            : this(new OracleDataContext())
        {
        }

        public UserStore(OracleDataContext database)
        {
            // TODO: Compare with EntityFramework provider.
            Database = database;
            _userRepository = new UserRepository(database);
        }

        public bool CreateRow(User row)
        {
            if (row == null)
            {
                throw new ArgumentException("CreateRow params row is null");
            }

            int result = _userRepository.AddUsser(row);
            if (result > 0)
                return true;

            return false;
        }

        public bool DeleteRow(User row)
        {
            throw new NotImplementedException();
        }

        public User FindByIdRow(string rowId)
                  //FindByIdRow
        {
            if (rowId.Equals(""))
            {
                throw new ArgumentException("userId");
            }

            var result = _userRepository.GetUserById(rowId);

            return result;
        }

        public ICollection<User> FindByIdRow(string userName, string password)
        {
            if (userName == null || userName.Equals(""))
            {
                throw new ArgumentException("userName");
            }

            if (password == null || password.Equals(""))
            {
                throw new ArgumentException("password");
            }

            var result = _userRepository.GetUserByUserPass(userName, password);

            return result;
        }

        public User FindBySql(string sSql)
        {
            throw new NotImplementedException();
        }

        public bool UpdateRow(User row)
        {
            throw new NotImplementedException();
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Database != null)
                {
                    Database.Dispose();
                    Database = null;
                }
            }
        }
    }
}
